<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
    #prompt {
        font-size: 12px;
        color: darkgray;
    }
    #score {
        border: solid 1px darkgray;
    }
    div .right {
        background: url() no-repeat 5px center;
        background-size: 15px 15px;
        padding-left: 20px;
        color: lightgreen !important;
        
    }
    div .error {
        background: url() no-repeat 5px center;
        background-size: 15px 15px;
        padding-left: 20px;
        color: red  !important;
        
    }
</style>
<body>
    <div id="box">
        <label for="score">您的成绩</label>
        <input type="text" placeholder="请输入您的成绩 " id="score">
        <span id="prompt" class="">请输入您的成绩</span>
    </div>
    <script>
        function $(id){
                    return typeof id==='string'?document.getElementById(id):null;
                }
        //输入框失去焦点
        $('score').onblur=function(){
            //获取输入内容
            var value=parseFloat(this.value);
        
            //验证
            if(isNaN(value)){
                $('prompt').innerHTML='输入的成绩不正确';
               // $('prompt').setAttribute('class','error');
                $('prompt').className=('error');
                this.style.borderColor='red';
            }else if(value>=0&&value<=100){
                $('prompt').innerHTML='您的成绩是'+value;
                $('prompt').setAttribute('class','right');
                this.style.borderColor='lightgreen';
            }else//超出范围
            {
                $('prompt').innerHTML='成绩必须在0~100之间';

                $('prompt').className=('error');
                this.style.borderColor='red';
            }
        }
        //input输入框获取焦点恢复原来状态
        $('score').onfocus=function(){
            $('prompt').innerHTML='请输入您的成绩'; 
            $('prompt').className='';  
            $('score').style.borderColor='darkgray';
            $('score').style.outline='none';     
            $('score').value='';
        }
    </script>
</body>
</html>